<template>
  <section>
    <div class="container">
      <div class="container-title">查询条件</div>
      <div class="container-body pd">
        <el-form
          label-position="right"
          label-width="120px"
          :model="searchForm"
          ref="searchForm"
          class="demo-form-inline"
        >
          <el-row :gutter="30">
            <el-col :span="5">
              <el-form-item label="商品名称：" size="small" prop="goodsName">
                <el-input
                  v-model="searchForm.goodsName"
                  placeholder="请输入商品名称"
                  prefix-icon="el-icon-search"
                  size="small"
                />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="详细地址：" size="small" prop="address">
                <el-input
                  v-model="searchForm.address"
                  placeholder="请输入详细地址"
                  prefix-icon="el-icon-search"
                  size="small"
                />
              </el-form-item>
            </el-col>

            <el-col :span="5">
              <el-form-item label="领取人姓名：" size="small" prop="name">
                <el-input
                  v-model="searchForm.name"
                  placeholder="请输入领取人姓名"
                  prefix-icon="el-icon-search"
                  size="small"
                />
              </el-form-item>
            </el-col>

            <el-col :span="5">
              <el-form-item label="领取人手机：" size="small" prop="phone">
                <el-input
                  v-model="searchForm.phone"
                  placeholder="请输入领取人手机"
                  prefix-icon="el-icon-search"
                  size="small"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="30">
            <el-col :span="5">
              <el-form-item label="快递单号：" size="small" prop="expressNum">
                <el-input
                  v-model="searchForm.expressNum"
                  placeholder="请输入快递单号"
                  prefix-icon="el-icon-search"
                  size="small"
                />
              </el-form-item>
            </el-col>

            <el-col :span="5">
              <el-form-item
                label="是否发货："
                size="small"
                prop="logisticsState"
              >
                <el-select
                  v-model="searchForm.logisticsState"
                  clearable
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item
                label="兑换起始时间："
                size="small"
                prop="starttime"
              >
                <el-date-picker
                  v-model="searchForm.starttime"
                  type="date"
                  placeholder="选择日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="兑换结束时间：" size="small" prop="endtime">
                <el-date-picker
                  v-model="searchForm.endtime"
                  type="date"
                  placeholder="选择日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="center">
            <el-button
              class="is-tableBtn"
              size="small"
              @click="clearForm('searchForm')"
              >清空</el-button
            >
            <el-button
              class="is-tableBtn"
              type="primary"
              size="small"
              @click="GetTbExchangeList(searchForm)"
              >查询</el-button
            >
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="container">
      <div class="container-title">兑换列表</div>
      <div class="container-body pd">
        <el-table
          :data="tableData"
          v-loading="loading"
          border
          style="width: 100%"
          size="small"
        >
          <el-table-column prop="cardNumber" label="卡编号" align="center">
          </el-table-column>
          <el-table-column prop="goodsName" label="商品名称" align="center">
          </el-table-column>
          <el-table-column prop="name" label="领取人姓名" align="center">
          </el-table-column>
          <el-table-column prop="phone" label="领取人手机" align="center">
          </el-table-column>
          <el-table-column prop="area" label="省市区" align="center">
          </el-table-column>
          <el-table-column prop="address" label="详细地址" align="center">
          </el-table-column>
          <el-table-column prop="logisticsStateName" label="物流状态" align="center">
          </el-table-column> 
          <el-table-column prop="num" label="数量" align="center">
          </el-table-column>
          <el-table-column prop="createTime" label="兑换时间" align="center">
          </el-table-column>
          <el-table-column label="操作" align="center" width="235">
            <template slot-scope="scope">
              <el-button
                class="btnwarning"
                type="warning"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >详情</el-button
              >
              <el-button
                v-if="scope.row.logisticsState!==1"
                class="btnwarning"
                type="warning"
                size="mini"
                @click="handleLogs(scope.$index, scope.row)"
                >快递信息</el-button
              >
              <el-button
                v-if="scope.row.logisticsState===1"
                class="btnwarning"
                type="danger"
                size="mini" 
                @click="opendeliver(scope.$index, scope.row)"
                >发货</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="pageBox">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            @current-change="currentPage"
          >
          </el-pagination>
          <div class="clear"></div>
        </div>
      </div>
    </div>
    <el-dialog
      title="物流信息"
      :visible.sync="showLogistics"
      width="30%"
      center
    >
      <ul class="logoistics" v-if="logisticsList.length > 0">
        <li v-for="(item, index) in logisticsList" :key="index">
          <span class="times">{{ item.time }}</span>
          <span class="status">{{ item.status }}</span>
        </li>
      </ul>
      <div class="nulltips">暂无物流记录</div>
    </el-dialog>
    <!-- 查询兑换详情弹窗 -->
    <el-drawer
      :visible.sync="createExchangePop"
      :append-to-body="true"
      custom-class="form-drawer"
      @close="close()"
    >
      <div class="drawer" slot="title">兑换详情</div>
      <div class="drawerContainer">
        <div class="drawerFormBox">
          <el-form
            :model="newExchangeForm"
            :rules="newExchangeRule"
            ref="newExchangeForm"
            label-width="150px"
            class="demo-neExchangeForm"
            size="small"
          > 
           <el-form-item label="商品名称：" prop="goodsName">
              <el-input
                v-model="newExchangeForm.goodsName"
                :disabled="true"
              ></el-input>
            </el-form-item>
           
            <el-form-item label="领取人姓名：" prop="name">
              <el-input
                v-model="newExchangeForm.name"
                :disabled="true"
              ></el-input>
            </el-form-item>

            <el-form-item label="领取人手机：" prop="phone">
              <el-input
                v-model.number="newExchangeForm.phone"
                :disabled="true"
              ></el-input>
            </el-form-item>

            <el-form-item label="省市区：" prop="area">
              <el-input
                v-model="newExchangeForm.area"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item label="详细地址：" prop="address">
              <el-input
                v-model="newExchangeForm.address"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item label="快递单号：" prop="expressNum">
              <el-input
                v-model="newExchangeForm.expressNum"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item label="快递类型：" prop="expressType">
              <el-input
                v-model="newExchangeForm.expressType"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item label="物流状态：" prop="logisticsStateName">
              <el-input
                v-model="newExchangeForm.logisticsStateName"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item label="数量：" prop="num">
              <el-input
                v-model="newExchangeForm.num"
                :disabled="true"
              ></el-input>
            </el-form-item>
           
            <el-form-item label="JD isv出库单号：" prop="isvUUID">
              <el-input
                v-model="newExchangeForm.isvUUID"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item label="JD 订单的出库单号：" prop="eclpSoNo">
              <el-input
                v-model="newExchangeForm.eclpSoNo"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="drawerBtnBox"> 
          <el-button
            type="primary"
            @click="resetFormESC('newGoodsForm')"
            size="small"
            >确 定</el-button
          >
        </div>
        </div>
      </div>
    </el-drawer>
    <el-dialog
      title="发货"
      :visible.sync="deliverPop"
      width="30%"
      @close="resetFormESC('deliverForm')"
    >
      <el-form
        label-position="right"
        label-width="100px"
        ref="deliverForm"
        :model="deliverForm"
        :rules="deliverRule"
        class="demo-form-inline"
      >
        <el-form-item label="快递公司：" prop="expressType">
          <el-select
            v-model="deliverForm.expressType"
            clearable
            placeholder="请选择快递"
            size="small"
          >
            <el-option
              v-for="item in deliverTypeList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号：" prop="expressNum">
          <el-input
            v-model="deliverForm.expressNum"
            placeholder="请输入快递单号"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetFormESC('deliverForm')">关 闭</el-button>
        <el-button type="primary" @click="deliverGoods('deliverForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </section>
</template>
<script>
export default {
  data() {
    return {
      page: 1,
      total: 0,
      deliverPop: false, //发货弹窗
      deliverId: "", // 发货id
      createExchangePop: false, // 商品创建弹窗是否展示
      deliverForm: {
        // 发货表单
        expressType: "",
        expressNum: "",
      },
      deliverTypeList: [], //快递类型列表
      deliverRule: {
        expressType: [
          { required: true, message: "请选择快递", trigger: "change" },
        ],
        expressNum: [
          { required: true, message: "请填写单号", trigger: "blue" },
        ],
      },
      newExchangeForm: {
        cardNumber: "",
        goodsName: "",
        num: 0,
        name: "",
        phone: "",
        area: "",
        address: "",
        expressNum: "",
        expressType: "",
        isDeliver: "",
        logisticsInfo: "",
        isvUUID: "",
        eclpSoNo: "",
        createTime: "",
      },
      showLogistics: false,
      newExchangeRule: {},
      options: [
        {
          value: false,
          label: "否",
        },
        {
          value: true,
          label: "是",
        },
      ],
      logisticsList: [], // 物流信息
      editID: "", // 当前编辑id
      tableData: [],
      loading: true,
      searchForm: {
        goodsName: "",
        address: "",
        name: "",
        phone: "",
        expressNum: "",
        isDeliver: "",
        starttime: "",
        endtime: "",
      },
    };
  },
  created() {
    this.GetTbExchangeList(this.searchForm);
    this.getRecevieType();
  },
  methods: {
    createNew() {
      this.createExchangePop = true;
      this.newExchangeForm = {
        cardNumber: "",
        goodsName: "",
        name: "",
        phone: "",
        area: "",
        address: "",
        expressNum: "",
        expressType: "",
        isDeliver: "",
        logisticsInfo: "",
        isvUUID: "",
        eclpSoNo: "",
        createTime: "",
      };
    },
    opendeliver(index, row) {
      this.deliverId = row.id;
      this.deliverPop = true;
    },
    async deliverGoods(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await this.$store.dispatch("putRecevied", {
            id: this.deliverId,
            expressType: this[formName].expressType,
            expressNum: this[formName].expressNum,
          });
          if (res.success) {
            this.$message({
              type: "success",
              message: "发货成功!",
            });
            this.GetTbExchangeList(this.searchForm);
          } else {
            this.$message.error(res.ErrorMessage || "网络异常");
          }
           this.deliverPop = false;
        }
      });
    },
    async getRecevieType() {
      let res = await this.$store.dispatch("getRecevieType", {});
      if (res.success) {
        this.deliverTypeList = res.data || [];
      }
    },
    async handleEdit(index, row) {
      let res = await this.$store.dispatch("GetTbExchange", {
        id: row.id,
      });
      if (res.success) {
        this.newExchangeForm = res.data || [];
      }

      this.createExchangePop = true;
    },
    async handleLogs(index, row) {
      let res = await this.$store.dispatch("GetLogistics", {
        id: row.id,
      });
      if (res.success) {
        this.logisticsList = res.data || [];
        this.showLogistics = true;
      }
    },
    currentPage(page) {
      this.page = page;
      this.GetTbExchangeList(this.searchForm);
    },
    clearForm(formName) {
      // 清空筛选
      try {
        this.$refs[formName].resetFields();
      } catch (error) {
        console.log(error);
      }
    },
    resetFormESC(formName) {
      // 充值表单
      this.createExchangePop = false;
      this.deliverPop = false;
      this.$refs[formName].resetFields();
    },
    async GetTbExchangeList(searchForm) {
      this.loading = true;
      let skip = (this.page - 1) * 10;
      // 请求案例
      let res = await this.$store.dispatch("GetTbExchangeList", {
        goodsName: searchForm.goodsName,
        address: searchForm.address,
        name: searchForm.name,
        phone: searchForm.phone,
        expressNum: searchForm.expressNum,
        isDeliver: searchForm.isDeliver,
        starttime: searchForm.starttime,
        endtime: searchForm.endtime,
        skip,
      });
      if (res.success) {
        this.tableData = res.data.itmes || [];
        this.total = res.data.totalCount;
      }
      this.loading = false;
    },
    close() {
      this.resetFormESC("newExchangeForm");
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/varibles.scss";
.btn {
  margin-bottom: $btnHeight;
  color: #ffff;
}
.btnwarning {
  color: $btnwarning;
}
.pageBox {
  padding-top: 10px;
  clear: both;
  .el-pagination {
    float: right;
  }

  .clear {
    clear: both;
  }
}
.logoistics {
  li {
    display: flex;
    .status {
      max-width: 300px;
    }
    justify-content: space-between;
    line-height: 50px;
    border-bottom: 1px solid #f4f4f4;
  }
}
.nulltips {
  padding: 30px;
  text-align: center;
  color: #999;
}
</style>
